package com.internetCafes.spms.web.customer.bizservice.impl.msg;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.common.utils.entity.page.PageItemDTO;
import com.internetCafes.spms.common.utils.entity.page.PageItemUtil;
import com.internetCafes.spms.common.utils.entity.page.PageItemVO;
import com.internetCafes.spms.web.customer.bizservice.msg.IEntMsgBizService;
import com.internetCafes.spms.web.customer.common.cont.CommonConst;
import com.internetCafes.spms.web.customer.common.cont.MsgConst;
import com.internetCafes.spms.web.customer.entity.msg.Msg;
import com.internetCafes.spms.web.customer.entity.msg.MsgUserAsso;
import com.internetCafes.spms.web.customer.model.msg.*;
import com.internetCafes.spms.web.customer.service.msg.IMsgService;
import com.internetCafes.spms.web.customer.service.msg.IMsgUserAssoService;
import com.sms.common.util.hutool.core.bean.BeanUtil;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import com.sms.common.util.hutool.core.date.DatePattern;
import com.sms.common.util.hutool.core.date.DateUtil;
import com.sms.common.util.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EntMsgBizServiceImpl implements IEntMsgBizService {

    @Autowired
    private IMsgService msgService;
    @Autowired
    private IMsgUserAssoService userAssoService;

    @Override
    public PageItemVO<MsgPagingRes> paging(PageItemDTO<MsgPagingReq> req) {
        Page<Msg> pagingDo = new Page<>(req.getPageNum(), req.getPageSize());
        MsgPagingReq conditions = req.getConditions();
        MsgPagingDo msgPagingDo = new MsgPagingDo();
        if (ObjectUtil.isNotNull(conditions)) {
            BeanUtil.copyProperties(conditions, msgPagingDo);
        }
        msgPagingDo.setReceiverId(ShiroUtils.getUserId());
        Page<MsgPagingPo> pagingPo = msgService.poPaging(pagingDo, msgPagingDo);

        PageItemVO<MsgPagingRes> pagingRes = PageItemUtil.toPageItemVO(pagingPo, new PageItemVO<>());

        pagingRes.setRecords(
                pagingPo.getRecords()
                        .stream()
                        .map(po -> {
                            MsgPagingRes res = new MsgPagingRes();
                            BeanUtil.copyProperties(po, res);
                            res.setCreateTime(DateUtil.format(po.getCreateTime(), DatePattern.NORM_DATETIME_PATTERN));
                            return res;
                        })
                        .collect(Collectors.toList()));

        if (CollectionUtil.isNotEmpty(pagingPo.getRecords())) {
            // 获取到的信息置为空
            userAssoService.update(
                    new LambdaUpdateWrapper<MsgUserAsso>()
                            .eq(MsgUserAsso::getReceiverId, ShiroUtils.getUserId())
                            .in(MsgUserAsso::getMsgId, pagingPo.getRecords().stream().map(MsgPagingPo::getId).collect(Collectors.toList()))
                            .set(MsgUserAsso::getReadFlag, CommonConst.FlagEnum.IS.getId()));
        }

        return pagingRes;
    }

    @Override
    public List<MsgListRes> list(MsgListReq req) {
        MsgListDo listDo = new MsgListDo();
        BeanUtil.copyProperties(req, listDo);
        if (ObjectUtil.isNull(req.getSize())) {
            listDo.setSize(MsgConst.DEFAULT_SIZE);
        }
        listDo.setReceiverId(ShiroUtils.getUserId())
                .setReadFlag(CommonConst.FlagEnum.NOT.getId());

        List<MsgListPo> listPo = msgService.list(listDo);

        if (CollectionUtil.isNotEmpty(listPo)) {
            // 获取到的信息置为空
            userAssoService.update(
                    new LambdaUpdateWrapper<MsgUserAsso>()
                            .eq(MsgUserAsso::getReceiverId, ShiroUtils.getUserId())
                            .in(MsgUserAsso::getMsgId, listPo.stream().map(MsgListPo::getId).collect(Collectors.toList()))
                            .set(MsgUserAsso::getReadFlag, CommonConst.FlagEnum.IS.getId()));
        }

        return listPo.stream()
                .map(po -> {
                    MsgListRes res = new MsgListRes();
                    BeanUtil.copyProperties(po, res);
                    res.setCreateTime(DateUtil.format(po.getCreateTime(), DatePattern.NORM_DATETIME_PATTERN));
                    return res;
                })
                .collect(Collectors.toList());
    }

    @Override
    public UnReadCountRes unReadCount() {
        return new UnReadCountRes(
                userAssoService.count(
                        new LambdaQueryWrapper<MsgUserAsso>()
                                .eq(MsgUserAsso::getReceiverId, ShiroUtils.getUserId())
                                .eq(MsgUserAsso::getReadFlag, CommonConst.FlagEnum.NOT)));
    }
}
